<?php
/**
 * 消息逻辑类
 * @author zengnz
 *
 */
class MessageLogic extends CommonModel
{
	// 消息分表数目
	private $_Msg_tables = 5;
	// 模型类
	private $_Model = '';
	// 缓存模型类
	private $_CacheModel = '';
	
	public function __construct() {
		$this -> _CacheModel = D('Cache', 'Service');
		$this -> _CacheModel -> init();
		$this -> _Model = new Model();
	}
	
	/**
	 *  发送系统消息
	 * @param unknown_type $to
	 * @param unknown_type $type
	 */
	public function systemMsg($to,$type=1) {
		$msg = '恭喜您，注册成功！希望本站能给你带来丰厚的收益，如有问题请致电13696913942，或QQ：407585136.';
		$ret = $this -> sendMsg(1, $to, $msg);
		return $ret;
	}
	
	/**
	 * 发送消息
	 * @param integer $from 发送方ID 1为系统消息
	 * @param integer $to 接收方ID
	 * @param string $msg  消息内容
	 * @param integer $type 类型0 系统消息
	 * @return -1参数错误，-2发送失败
	 */
	public function sendMsg($from, $to, $msg) {
		if (!$from || !$to || !$msg)
			return -1;
		
		$UserLogic = D('User', 'Logic');
		$toInfo = $UserLogic -> getUserBaseInfo($to);
		$tName = $toInfo ? $toInfo['account'] : 'NULL';
		$fName = 'admin';
		if ($from != 1) {
			$fromInfo = $UserLogic -> getUserBaseInfo($from);
			$fName = $fromInfo ? $fromInfo['account'] : 'NULL';
		}
		
		$data = array(
			'f_id' 		=> $from,
			'f_name'    => $fName,	
			't_id'   	=> $to,
			't_name'	=> $tName,
			'content' 	=> $msg,
			'create_time' => time(),
		);
		
		// 非系统消息,先保存发送方消息，再保存收件方消息
		if ($from != 1) {
			$fromSuffix = $from % $this -> _Msg_tables;
			$data['status'] = 0;
			$fromRet = $this -> _Model -> table('message_'.$fromSuffix)
					-> add($data);
			if (!$fromRet)
				return -2;
			
			$fromId = $this -> _Model -> getLastInsID();
		}
		
		
		$toSuffix = $to % $this -> _Msg_tables;
		$data['status'] = 2;
		
		$toRet = $this -> _Model -> table('message_'.$toSuffix)
				-> add($data);
		if (!$toRet)
			return -2;
		
		// 非系统消息，接收方收到消息后，发送方状态变为已发送
		if ($toRet && $from != 1) {
			$updata = array('status'=>1);
			$ret = $this -> _Model -> table('message_'.$fromSuffix)
					-> where('id='.$fromId)
				    -> save($updata);
		}
		
		return 1;
	}
	
	/**
	 * 获取消息数目
	 * @param integer $uId
	 * @param integer $type 1收件箱，2发件箱，3系统消息
	 */
	public function getMsgCount($uId, $type=1) {
		if ($type == 1)
			$sql = 'f_id = ' . $uId;
		else if ($type == 2)
			$sql = 't_id = ' . $uId . ' AND f_id != 1';
		else
			$sql = 't_id = ' . $uId . ' AND f_id = 1';
		
		$tbSuffix = $uId % $this -> _Msg_tables;
		$count = $this -> _Model -> table('message_'.$tbSuffix)
				 -> where($sql)
				 -> count();	
		
		return $count;
	}
	
	/**
	 * 
	 * @param integer $uId
	 * @param integer $type 类型
	 * @param integer $from
	 * @param integer $listRows
	 * @return array
	 */
	public function getMegList($uId, $type=1, $from, $listRows) {
		if ($type == 1)
			$sql = 'f_id = ' . $uId;
		else if ($type == 2)
			$sql = 't_id = ' . $uId . ' AND f_id != 1';
		else
			$sql = 't_id = ' . $uId . ' AND f_id = 1';
		
		$tbSuffix = $uId % $this -> _Msg_tables;
		$list = $this -> _Model -> table('message_'.$tbSuffix)
				-> where($sql)
				-> limit($from, $listRows)
				-> select();
		
		return $list;
	}
	
}